import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

const ComLogin = () => import('@/components/loginCom.vue')
const WelCome = () => import('@/components/WelCome.vue')
const UserList = () => import('@/components/users/UserList.vue')
const Roles = () => import('@/components/rights/Roles.vue')
const Rights = () => import('@/components/rights/Rights.vue')
const Cate = () => import('@/components/goods/Cate.vue')
const Params = () => import('@/components/goods/Params.vue')
const GoodsList = () => import('@/components/goods/GoodsList.vue')
const AddGoods = () => import('@/components/goods/AddGoods.vue')
const Orders = () => import('@/components/orders/Orders.vue')
const Reports = () => import('@/components/report/Reports.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: WelCome
      },
      {
        path: '/users',
        component: UserList
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: GoodsList
      },
      {
        path: '/goods/add',
        component: AddGoods
      },
      {
        path: '/orders',
        component: Orders
      },
      {
        path: '/reports',
        component: Reports
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: ComLogin
  }
]

const router = new VueRouter({
  routes
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  const path = to.path
  // token
  const token = window.sessionStorage.getItem('token')
  // 去登录页面直接放行
  if (path === '/login') return next()
  // 没有token去其他页面不放行 跳转到登录页面
  if (!token) return next('/login')
  next()
})

export default router
